
'''
    Software License

    Copyright (C) 2021-05-24  Xoronos

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, version 3.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
'''

'''
    Liabilities

    The software is provided "AS IS" without any warranty of any kind, either expressed,
    implied, or statutory, including, but not limited to, any warranty that the software
    will conform to specifications, any implied warranties of merchantability, fitness
    for a particular purpose, and freedom from infringement, and any warranty that the
    documentation will conform to the software, or any warranty that the software will
    be error free.

    In no event shall Xoronos be liable for any damages, including, but not limited to,
    direct, indirect, special or consequential damages, arising out of, resulting from,
    or in any way connected with this software, whether or not based upon warranty,
    contract, tort, or otherwise, whether or not injury was sustained by persons or
    property or otherwise, and whether or not loss was sustained from, or arose out of
    the results of, or use of, the software or services provided hereunder.
    
    To request the provided software under a different license you can contact us at
    support@xoronos.com
'''

#####################################################
##                   Introduction                  ##
#####################################################
## This code example shows how to check for the    ##
## validity of Decompressed Xoron matrices, or     ##
## Xoron matrices.                                 ##
#####################################################

#####################################################
##                   Pre-requisits                 ##
#####################################################
## The Xoron Matrix Decompressed and Xoron Matrix  ##
## Decompressed need to be available before        ##
## running this code example.                      ##
## Some examples on how to generate the            ##
## aforementioned data structures can be found in  ##
## generate_xrn_matrix_all_rnd.py or               ##
## generate_xrn_matrix_one_third.py                ##
#####################################################

import xrnlib256 as xrn

##########################
## Variable definitions ##
##########################

# Xoron matrix representations
#
# The Xoron matrix compressed is the output of the mining process.
# It needs to be decompressed to be used in security algorithms.
# Users can permute the Xoron matrix compressed data-structure to derive
# many Xoron matrices.

xmc = xrn.xrn_matrix_compressed_t()

# The Xoron matrix is the modular part of Xoron cryptography, and therefore
# it is used directly to perform cryptography.
# The Xoron matrix is derived from a Xoron matrix comrpessed data structure. 

xm = xrn.xrn_matrix_t();

# The encoding settings are used to load or store file produced by the Xoron
# cryptography

settings = xrn.xrn_crypto_extra_settings_t( ) ;

# settings.encoding_settings.i/o_settings.mode = (std_parameters/xrn_parameters/std_parameters_plus_password/xrn_parameters_plus_password)
#                     std_parameters the compiled settings
#                     xrn_parameters the settings coming from a file
#                     std_parameters_plus_password compiled parameters with a password
#                     xrn_parameters_plus_password file parameters with a password
# i/o_settings.encoding_parameters_fp file pointer if mode is ( xrn_parameters/xrn_parameters_plus_password )
# i/o_settings.password[XRN_ENC_PASS_BUFF_SIZE] password string if mode is ( xrn_parameters_plus_password/std_parameters_plus_password )
# i/o_settings.password_length the password length if the mode is ( xrn_parameters_plus_password/std_parameters_plus_password )

#############
## Logging ##
#############

# Set the default logging file streams
# errors -> stderr
# warnings -> stdout
# notifications -> stdout
# debug -> stdout

xrn.xrn_set_default_log(  );

# To change the default logging you can use the functions below,
# where fp is the opened file pointer
# 
# xrn_change_error_log( fp )
# xrn_change_warning_log( fp )
# xrn_change_notification_log( fp )
# xrn_change_debug_log( fp )

#############################
## Variable initialization ##
#############################

# use standard settings without a password to load Xoron files
settings.encoding_settings.i_settings.mode = xrn.std_parameters;
settings.encoding_settings.i_settings.check_mode = xrn.xrn_automatic;

# load Xoron matrix compressed

compressed_xmatrix_fp = xrn.fopen( "xrn_matrix_compressed.xmc", "rb" )
if ( compressed_xmatrix_fp == None ) :
    exit ( -1 )

if ( xrn.XSUCCESS != xrn.xrn_load_matrix_compressed( compressed_xmatrix_fp, xmc, settings ) ) :
    xrn.fclose( compressed_xmatrix_fp )
    exit ( -1 )

if ( 0 != xrn.fclose( compressed_xmatrix_fp ) ) :
    exit ( -1 )

# load Xoron matrix decompressed

decompressed_xmatrix_fp = xrn.fopen( "xrn_matrix.xm", "rb" )
if ( decompressed_xmatrix_fp == None ) :
    xrn.fclose( decompressed_xmatrix_fp )
    exit ( -1 )

if ( xrn.XSUCCESS != xrn.xrn_load_matrix( decompressed_xmatrix_fp, xm, settings ) ) :
    xrn.fclose( decompressed_xmatrix_fp )
    exit ( -1 )

if ( 0 != xrn.fclose( decompressed_xmatrix_fp ) ) :
    exit ( -1 )

###########
## Check ##
###########

print( "check Xoron matrix compressed " )

if ( xrn.XSUCCESS != xrn.xrn_matrix_compressed_check( xmc ) ) :
    print( "failed" )
else :
    print( "passed" )

print( "check Xoron matrix " )

if ( xrn.XSUCCESS != xrn.xrn_matrix_check( xm ) ) :
    print( "failed" )
else :
    print( "passed" )

exit ( 0 )

